package com.leyou.lyelasticsearch.controller;

import com.leyou.item.entity.TbCategory;
import com.leyou.lyelasticsearch.entity.SearchRequest;
import com.leyou.lyelasticsearch.entity.SearchResult;
import com.leyou.lyelasticsearch.service.SearchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class SearchController {

    @Autowired
    private SearchService searchService;

    /**
     * 商品的展示
     * @param searchRequest
     * @return
     */
    @PostMapping("page")
    public ResponseEntity<SearchResult> searchGoodsByPage(@RequestBody SearchRequest searchRequest){
        SearchResult goodsMyPage = searchService.searchGoodsByPage(searchRequest);
        if(goodsMyPage==null){
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(goodsMyPage);
    }

    /**
     * 面包屑
     */
    @GetMapping("breads")
    public ResponseEntity<List<TbCategory>> queryCategoriesBySpuId(@RequestParam("id")Long id3){
        List<TbCategory> tbCategories = searchService.queryCategoriesById3(id3);
        if(tbCategories==null){
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(tbCategories);
    }
}
